/*************************************************************************
 * Tic Tac Toe
 *
 * Author: Justin Sheppard
 * Student #: 200811917
 * E-Mail: justin.sheppard@mun.ca
 * ***********************************************************************/

package tictactoe;
import java.util.Scanner;

/** ConsoleRunner *************************************************************
 * 
 * This class is used to deal with all input and output from/to the console
 * during the game, including: AI Moves, Player moves, and the outputting
 * of the game's status (draw, win, lose).
 * 
 * ****************************************************************************/
public class ConsoleRunner {
	public Game game;
	private boolean playerIsX;
	
	/** Constructor ********************************************************************
	 * 
	 * Instantiates a new game and sets this class's playerIsX to that which was determined
	 * in TicTacToeApplication.
	 * 
	 * @pre		-	
	 * @params	- playerIsX: True if X, false if O.
	 * 			  challenging: True if player wanted to play against a tougher AI.
	 * @returns - 
	 * @post	- A new game is created and playerIsX is set.
	 * 
	 * ****************************************************************************/
	public ConsoleRunner(boolean playerIsX, boolean challenging){
		game = new Game(playerIsX, challenging);	
		this.playerIsX = playerIsX;
	}
	
	/** playerMove ********************************************************************
	 * 
	 * Contains everything that will occur when it is the player's turn to move. 
	 * 
	 * @pre		-	
	 * @params	- 	
	 *
	 * @returns - 
	 * @post	- Player's move is output to the console.
	 * ****************************************************************************/
	private void playerMove(){
		if(game.getStatus() == GameStatus.IN_PROGRESS){	//Only move if in progress, otherwise game has already ended.
			Scanner playerPos = new Scanner(System.in); 
			int i, j; //(x, y) coordinates of the desired position to place a piece.

			System.out.println("Enter desired x-coordinate:");
			i = playerPos.nextInt();

			System.out.println("Enter desired y-coordinate:");
			j = playerPos.nextInt();

			if(game.placePlayerPiece(i, j)){ //If piece placed successfully
				System.out.println("After your move:");	//Output it to the console
				System.out.println(game.getBoard().toString()); //Prints the board
										//Player is X and X has 3 in a row			//Player is O and O has 3 in a row
				if((game.getStatus() == GameStatus.X_WON && playerIsX == true) || (game.getStatus() == GameStatus.O_WON && playerIsX == false))
					System.out.println("You Won!");
			}else{ //If not placed successfully, it was an invalid move, and user asked to try again.
				System.out.println("Invalid move. Please try again.");
				playerMove();
			}
		}else if(game.getStatus() == GameStatus.DRAW) //Not in progress, and winning conditions already checked
			System.out.println("Draw.");
	}
	
	/** aiMove ********************************************************************
	 * 
	 * Contains everything that will occur when it is the AI's turn to move. 
	 * 
	 * @pre		-	
	 * @params	- 	
	 *
	 * @returns - 
	 * @post	- AI's move is output to the console.
	 * ****************************************************************************/
	private void aiMove(){
		if(game.getStatus() == GameStatus.IN_PROGRESS){	//Only move if game is in progress
			System.out.println("After AI move:");
			game.aiPlacePiece();	//Incorrect placement is handled within the AI.
			System.out.println(game.getBoard().toString());
									//Player is O and X has 3 in a row			//Player is X and O has 3 in a row
			if((game.getStatus() == GameStatus.X_WON && playerIsX == false) || (game.getStatus() == GameStatus.O_WON && playerIsX == true))
				System.out.println("You lose.");	//AI has won
			
		}else if(game.getStatus() == GameStatus.DRAW){
			System.out.println("Draw.");
		}
	}
	
	/** mainLoop ********************************************************************
	 * 
	 * Using playerMove and aiMove, prints the alternating moves to the console, as
	 * long as the game is in progress. Assumed that X goes first.
	 * 
	 * @pre		-	
	 * @params	- 	
	 *
	 * @returns - 
	 * @post	- Player's and AI's moves are output to the console in correct order
	 * 			  while the game is in progress.
	 * ****************************************************************************/
	public void mainLoop(){
		System.out.println(game.getBoard().toString() + '\n');
		if(playerIsX)
			while(game.getStatus() == GameStatus.IN_PROGRESS){
				playerMove();		
				aiMove();
			}
		else
			while(game.getStatus() == GameStatus.IN_PROGRESS){
				aiMove();
				playerMove();
			}
	}
}
